<?php
	include("../../include/dbconnect.php");
	extract($_POST);
	
	$table="dvthang";
	$table_title="đơn vị tính cho hàng hóa";
	$table_title_u="Đơn vị tính cho hàng hóa";
	
	session_start();
	header("Content-Type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
	echo "<$table>";
	
	function valid()
	{
		global $table,$idhang,$id,$cw,$conn,$table_title,$table_title_u;
		if($_SESSION["un"]=="")
		{
			echo "<status>fail</status>";
			echo "<message>Chưa đăng nhập !</message>";
			return false;
		}
		elseif($idhang=="")
		{
			echo "<status>fail</status>";
			echo "<message>Phải chọn hàng hóa !</message>";
			return false;
		}
		elseif($id=="")
		{
			echo "<status>fail</status>";
			echo "<message>Phải chọn đơn vị cần nhập !</message>";
			return false;
		}
		elseif($cw!="true" && $cw!="false")
		{
			echo "<status>fail</status>";
			echo "<message>Phải chọn có nhập cả đơn vị dẫn xuất không !</message>";
			return false;
		}
		else
		{
			$err=false;
			$rs=mysql_query("select * from hang where id=$idhang",$conn);
			if(mysql_num_rows($rs)<=0)
			{
				echo "<status>fail</status>";
				echo "<message>Không có $table_title [$id] !</message>";		
				$err=true;
			}
			mysql_free_result($rs);
			if($err) return false;

			$rs=mysql_query("select * from dvtbuiltin where id=$id",$conn);
			if(mysql_num_rows($rs)<=0)
			{
				echo "<status>fail</status>";
				echo "<message>Không có đơn vị cài sẵn mã [$id] !</message>";		
				$err=true;
			}
			mysql_free_result($rs);
			if($err) return false;
		}
		return true;
	}// valid
	
	function root($id)
	{
		global $conn,$table;
		$iddvtbuiltin="";
		$rs=mysql_query("select * from dvtbuiltin where id=$id",$conn);
		while($a=mysql_fetch_array($rs)) $iddvtbuiltin=$a["iddvtbuiltin"];
		mysql_free_result($rs);
		if($iddvtbuiltin=="") return $id;
		return root($iddvtbuiltin);
	}
	if(valid())
	{
		$rs=mysql_query("select * from dvtbuiltin where id=$id",$conn);
		$ten="";
		while($a=mysql_fetch_array($rs)) $ten=$a["ten"];
		mysql_free_result($rs);
		
		$sql="insert into $table (ten,idhang,tyle,iddvthang,chophep,un,iddvtbuiltin) values (";
		$sql=$sql."'$ten',$idhang,null,null,1,'".$_SESSION["un"]."',$id)";
		mysql_query($sql,$conn) or die($sql);
		$iddvthang=mysql_insert_id($conn);
		if($cw=="true")
		{
			$rs=mysql_query("select * from dvtbuiltin",$conn);
			while($a=mysql_fetch_array($rs))
			{
				if($a["id"]!=$id)
				if(root($a["id"])==$id)
				{
					$sql="insert into $table (ten,idhang,tyle,iddvthang,chophep,un,iddvtbuiltin) values (";
					$sql=$sql."'".$a["ten"]."',$idhang,".$a["tyle"].",null,1,'".$_SESSION["un"]."',".$a["id"].")";
					mysql_query($sql,$conn);
				}
			}
			mysql_free_result($rs);
			$rs=mysql_query("select * from dvtbuiltin",$conn);
			while($a=mysql_fetch_array($rs))
			{
				if($a["iddvtbuiltin"]!="")
				{
					// id dvthang có dvtbuiltin
					$id1="";
					$rs1=mysql_query("select * from $table where iddvtbuiltin=".$a["id"],$conn);
					while($a1=mysql_fetch_array($rs1)) $id1=$a1["id"];
					mysql_free_result($rs1);

					// id dvtbuiltin la cha $a["iddvtbuiltin"]
					$id2="";
					$rs1=mysql_query("select * from dvtbuiltin where id=".$a["iddvtbuiltin"],$conn);
					while($a1=mysql_fetch_array($rs1)) $id2=$a1["id"];
					mysql_free_result($rs1);
					
					$id3="";
					$rs1=mysql_query("select * from $table where iddvtbuiltin=$id2",$conn);
					while($a1=mysql_fetch_array($rs1)) $id3=$a1["id"];
					mysql_free_result($rs1);
					
					mysql_query("update $table set iddvthang=$id3 where id=$id1",$conn);
				}
			}
			mysql_free_result($rs);
		}
		echo "<status>ok</status>";
		echo "<message>Đã nhập $table_title [",$ten,"]!</message>";
		echo "<id>$iddvthang</id>";
	}
	echo "</$table>";
	include("../../include/dbclose.php");
?>